package com.cwt_taxi_xa.timer;

import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.protocol.DispatchCenterUsers;
import com.cwt_taxi_xa.util.AppLog;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;

/**
 * Created by ZQX on 2019/7/13 0013 10:53
 */
public class ThreanReTime implements Runnable {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    long common_l = 0, heart_l = 0;
    int common_i = 0, heart_i = 0;

    File RecordRunFile = null;
    FileOutputStream fos_rrf;
    static String path_debug = "/mnt/sdcard/导航终端连续运行记录/";
    int recordindex = 55;
    long lastRecordTime = 0;

    @Override
    public void run() {
        while (true){
            try{
                Thread.sleep(1000);

                /**
                 * 2020-4-17
                 * 记录程序运行的连续性
                 */
                if(recordindex>=60){
                    recordindex = 0;
                    recordRun();
                }else{
                    recordindex++;
                }





                /**
                 * 刷新顶层界面时间
                 */
                if (MainApplication.getInstance().topActivity != null) {
                    MainApplication.getInstance().topActivity.sendMsg(1002, sdf.format(new Date()));
                }


                if(common_l==TimerCommon.lastReTime){
                    common_i++;
                }else{
                    common_l = TimerCommon.lastReTime;
                    common_i = 0;
                }
                if(common_i>10){
                    try {
                        AppLog.savelog("TimerCommonReTime检测到通用线程停止，重新启动---1");
                        TimerCommon.lastReTime = System.currentTimeMillis();
                        MainApplication.getInstance().mtimer.cancel();
                        MainApplication.getInstance().mtimer = null;
                        MainApplication.getInstance().mTimerCom.cancel();
                        MainApplication.getInstance().mTimerCom = null;
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    Thread.sleep(2000);
                                } catch (Exception e) {
                                }
                                MainApplication.getInstance().mTimerCom = new TimerCommon();
                                MainApplication.getInstance().mtimer = new Timer();
                                MainApplication.getInstance().mtimer.schedule(MainApplication.getInstance().mTimerCom, 1000, 1000);
                            }
                        }).start();
                        AppLog.savelog("TimerCommonReTime检测到通用线程停止，重新启动---1");
                    } catch (Exception e) {
                    }
                }


                if(heart_l==TimerHeartBeat.lastReTime){
                    heart_i++;
                }else{
                    heart_l = TimerHeartBeat.lastReTime;
                    heart_i = 0;
                }
                if(heart_i>10){
                    try {
                        // 启动心跳维持连接
                        AppLog.savelog("TimerCommonReTime检测到心跳线程停止，重新启动---1");
                        TimerHeartBeat.lastReTime = System.currentTimeMillis();
                        DispatchCenterUsers.startHeartBeat();
                        AppLog.savelog("TimerCommonReTime检测到心跳线程停止，重新启动---2");
                    } catch (Exception e) {
                    }
                }





            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private void recordRun() {
        try {
            if (fos_rrf == null) {
                RecordRunFile = new File(path_debug);
                if (!RecordRunFile.exists()) {
                    RecordRunFile.mkdir();
                }
                RecordRunFile = new File(RecordRunFile, "新纪录.txt");
                if (RecordRunFile.exists() && RecordRunFile.length() > 1024 * 1024 * 5) {
                    File f = new File("/mnt/sdcard/导航终端连续运行记录/历史记录.txt");
                    if (f.exists()) {
                        f.delete();
                    }
                    RecordRunFile.renameTo(f);
                }
                RecordRunFile = new File(RecordRunFile, "新纪录.txt");
                fos_rrf = new FileOutputStream(RecordRunFile, true);
                fos_rrf.write(("\n\n-----------------------------打开输出流---------------------------").getBytes());
            }
            if(lastRecordTime==0){
                fos_rrf.write(("\n"+"新纪录"+sdf.format(new Date())).getBytes());
            }else{
                fos_rrf.write(("\n"+"记录间隔:"+(System.currentTimeMillis()-lastRecordTime)+"      "+sdf.format(new Date())).getBytes());
            }
            lastRecordTime = System.currentTimeMillis();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
